home *** CD-ROM | disk | FTP | other *** search
- Path: fnpx20.fnal.gov!not-for-mail
- From: sfield@fnpx20.fnal.gov (Stephen Field)
- Newsgroups: comp.lang.c
- Subject: Q: Terminating program at EOF
- Date: 6 Jan 1996 19:04:19 -0600
- Organization: FERMILAB, Batavia, IL
- Message-ID: <4cn66j$5r0@fnpx20.fnal.gov>
- NNTP-Posting-Host: fnpx20.fnal.gov
- Summary: help
- X-Newsreader: NN version 6.5.0 #4 (NOV)
-
- Hi,
-
- I'm writing a program that looks at poorly formatted text files and reformats
- them to a user-specified line length. The input text file has a blank line
- between paragraphs and I would like the newly formatted file to also have blank
- lines between paragraphs.
-
- I've written a program that does the job, but it doesn't stop when it reaches
- the end of the file. The program will write out the reformatted file, but it
- appends a lot of "extra" characters to the end of the file and only stops when
- I break out of it.
-
- I have tried this program on many files and it behaves the same. The program
- is included below and I've got it running on an SGI running IRIX.
-
- Any help appreciated.
-
- Steve
-
- ===================================================
- /* This program reformats a file that is input by the user to not exceed
- a number of columns that is also input and outputs the reformatted file
- to a file also given by the user at runtime. Lines are broken at spaces. */
- #include <stdio.h>
- #define MAX_WORD_LENGTH 80 /* max length of word in input */
-
- main()
- {
- int line_length, /* max length of line in output */
- curr_line_length = 0, /* # of chars printed so far in curr line */
- word_length; /* length of current word */
- char word[ MAX_WORD_LENGTH+1];/* buffer to read word +1 for terminating
- '\0' */
- char c_old=' ',c_new=' '; /* check for newlines with chars */
- FILE *fptr_read,*fptr_write; /* file to write to */
-
- char translate[3]={'\'','\"','\"'}; /* translate "wierd" chars to ASCII */
-
- printf("Enter file name to read: "); /* Get the input file */
- scanf("%s",word);
- fptr_read=fopen(word,"r"); /* open the file, don't check */
-
- printf("Enter line length: ");
- scanf("%d",&line_length); /* input new line length */
-
- printf("Enter file name to output to: ");
- scanf("%s",word); /* file name to output to */
- fptr_write=fopen(word,"w");
-
- /* while((c_new=fgetc(fptr_read))!=EOF){ */ /* old method for removing EOF bug */
- while(c_new!=EOF){ /* new method, still doesn't work */
- c_new=fgetc(fptr_read);
- if(c_new=='\n'){ /* if fgetc sees a \n, there's 2 \n's in a row */
- fprintf(fptr_write,"\n"); /* make sure paragraphs are sep. by a line */
- curr_line_length=0; /* reset the line length */
- continue; /* get the next char */
- }
- else {
- ungetc(c_new,fptr_read); /* it's not a \n, so put it back for fscanf */
- }
- if(fscanf(fptr_read,"%s",word)==EOF) /* try to fix EOF bug here */
- break;
- /* determine length of current word */
- for(word_length = 0 ; word[word_length]!='\0';
- word_length++)
- word[word_length]=word[word_length]>=146? /* translate wierd chars here */
- translate[word[word_length]-146]:word[word_length];
-
- if(word_length>line_length)
- printf("\nERROR: word length exceeds line length!!");
- if(curr_line_length==0){ /* first word to write? */
- fprintf(fptr_write,"%s",word);
- curr_line_length=word_length;
- }
- else {
- /* +1 is for space */
- curr_line_length += word_length + 1;
- /* if word won't fit on current line,
- start a new line */
- if(curr_line_length>line_length){
- fprintf(fptr_write,"\n%s",word);
- curr_line_length=word_length;
- }
- else
- fprintf(fptr_write," %s",word); /* word fits, add space and
- word to current line */
- }
- }
- }
-
-